package com.ericsson.enap.modules.cdr.thread;

import com.ericsson.common.pojo.FTPInfoVO;
import com.ericsson.common.util.DateUtils;
import com.ericsson.common.util.FtpCarrierUtil;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * 下载IMSCDR线程（根据IMSCG划分）
 */
public class IMSOriginalCDRDownLoadThread implements Runnable{

    private static Logger log = LoggerUtils.Logger(LogFileName.ImsOriginalCdrDownLoadJob);
    private FTPInfoVO ftpInfo;
    private String imsDownloadDir;

    public IMSOriginalCDRDownLoadThread(FTPInfoVO ftpInfo, String imsDownloadDir) {
        this.ftpInfo = ftpInfo;
        this.imsDownloadDir=imsDownloadDir;
    }
    @Override
    public void run() {
        String dirpath = ftpInfo.getPath();
        log.info("begin start-----------------------" + " dirpath :" + dirpath + "---------------------");

        FtpCarrierUtil ftpCarrier = new FtpCarrierUtil(ftpInfo,log);
        try {
            ftpCarrier.connect();
        } catch (Exception e1) {
            log.info("CDRDownLoadTask>>FTP connect error ================== "
                    + e1.getMessage());
        }
        String beforeDate=DateUtils.beforNumDay(new Date(),-1);
        ftpCarrier.download_ORGINALIMSCDR(imsDownloadDir+File.separator, beforeDate);
        try {
            ftpCarrier.close();
        } catch (IOException e) {
            log.error("CDRDownLoadTask close ftp connection IO Exception：" + e.getMessage() + " dirpath :" + dirpath);
        }
        log.info(" ===== CDRDownLoadTask execute  end============="  +" dirpath :" + dirpath);

        log.info("end stop-----------------------" + " dirpath :" + dirpath + "---------------------");
    }
}
